package psn.hui.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.Role;
import psn.hui.sys.domain.User;
import psn.hui.sys.mapper.UserMapper;
import psn.hui.sys.service.UserService;
import psn.hui.sys.vo.UserVo;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 	用户登录
	 * @param userVo
	 * @return
	 */
	@Override
	public User login(UserVo userVo) {
		//md5加密密码
		String md5pwd = DigestUtils.md5DigestAsHex(userVo.getPwd().getBytes());
		userVo.setPwd(md5pwd);
		return userMapper.login(userVo);
	}
	
	/**
	 * 	动态sql插入用户数据
	 */
	@Override
	public int insertSelective(User record) {
		//设置默认密码
		record.setPwd(DigestUtils.md5DigestAsHex(SysConstant.DEFAULT_PWD.getBytes()));
		//设置用户类型 都是普通用户 2
		record.setType(SysConstant.USER_TYPE_ADMIN);
		return userMapper.insertSelective(record);
	}
	/**
	 * 	根据用户id修改用户数据
	 */
	@Override
	public int updateByPrimaryKeySelective(User record) {
		return userMapper.updateByPrimaryKeySelective(record);
	}
	/**
	 *	 根据用户id删除用户数据，并关联删除sys_role_user表的数据
	 */
	@Override
	public int deleteById(Integer id) {
		//根据用户id删除用户数据
		int rs = userMapper.deleteByPrimaryKey(id);
		//关联删除sys_role_user表的数据
		rs += userMapper.deleteRoleUserByUid(id);
		return rs;
	}
	/**
	 * 	条件查询用户列表
	 */
	@Override
	public List<User> queryAllUserForList(User user) {
		return userMapper.queryAllUserForList(user);
	}
	
	/**
	 * 	根据角色id批量删除角色数据
	 */
	@Override
	public int batchDeleteUserByIds(Integer[] ids) {
		int rs = 0;
		for (Integer id : ids) {
			rs += deleteById(id);
		}
		return rs;
	}
	/**
	 * 	重置用户密码
	 * @param userid
	 * @return
	 */
	@Override
	public int resetUserPwd(Integer userid) {
		User user = new User();
		user.setUserid(userid);
		user.setPwd(DigestUtils.md5DigestAsHex(SysConstant.DEFAULT_PWD.getBytes()));
		return userMapper.updateByPrimaryKeySelective(user);
	}
	/**
	 * 	检查用户的登录名是否可用(不重复)，计数
	 */
	@Override
	public int checkLoginName(String loginname) {
		return userMapper.countByLoginName(loginname);
	}
	
	/**
	 * 	保存分配用户角色数据（用户角色关系表）,会先删除该用户旧的分配数据
	 * @param userVo
	 * @return
	 */
	public int saveUserRole(UserVo userVo) {
		Integer userid = userVo.getUserid();
		Integer[] rids = userVo.getIds();
		//移除该用户旧的角色分配数据
		int rs = userMapper.deleteRoleUserByUid(userid);
		//保存该用户新的角色分配数据
		for (Integer rid : rids) {
			rs += userMapper.insertUserRole(userid, rid);
		}
		return rs;
	}
	
	/**
	 * 	用户修改密码
	 * @param user
	 * @param password
	 * @return
	 */
	public boolean changePwd(User user, String password) {
		user.setPwd(DigestUtils.md5DigestAsHex(user.getPwd().getBytes()));
		User login = userMapper.login(user);
		if(null == login) {
			return false;
		}
		user.setPwd(DigestUtils.md5DigestAsHex(password.getBytes()));
		int rs = userMapper.updateByPrimaryKeySelective(user);
		return rs > 0;
	}
}
